#pragma once
#include <iostream>
#include <vector>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <thread>
#include <atomic>
#include <unordered_map>
#include <string>
#include <utility>
#include <random>

class EchoClient {
public:
    EchoClient(const char* ip, int port, int numConnections, int maxEchoCount = 3);
    ~EchoClient();
    bool init();
    void start();

private:
    void createConnections();
    void eventLoop();
    int connectToServer(int connId);
    void setNonBlocking(int fd);
    bool checkConnectSuccess(int fd);
    int recvAll(int fd, char* buffer, int expectedLen);
    std::string generateTestData(int connId, int echoCount); // 新增：带交互次数的测试数据

    std::string ip_;
    int port_;
    int numConnections_;
    int epollFd_;
    std::atomic<int> connected_{0};
    std::atomic<int> completed_{0}; // 完成所有交互的连接数
    const int maxEchoCount_; // 每个连接最大交互次数
    // 存储每个连接的状态 (fd -> (发送的数据, 已完成交互次数))
    std::unordered_map<int, std::pair<std::string, int>> connState_;
    std::mt19937 rng_;
    const std::string charSet_ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=";
    static const int MAX_EVENTS = 1024;
    static const int BUFFER_SIZE = 512;
};
